<script setup lang="ts">
import { Form } from '@/components/Form'
import {ElRow, ElCol, ElButton} from 'element-plus'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch } from 'vue'
import { TableData } from '@/api/table/types'
import { useI18n } from '@/hooks/web/useI18n'
import { useValidator } from '@/hooks/web/useValidator'
import { IDomEditor } from '@wangeditor/editor'
import { FormSchema } from '@/types/form'

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<TableData>>,
    default: () => null
  }
})

const { t } = useI18n()

const schema = reactive<FormSchema[]>([
  {
    field: 'imgTitle',
    label: t('名称'),
    component: 'Input',
    formItemProps: {
      rules: [required()]
    },
    colProps: {
      span: 24
    }
  },
  {
    field:'createUser',
    label:'创建人',
    component:'Input'
  },
  {
    field: 'display',
    label: t('上线/下线'),
    component: 'Radio',
    componentProps: {
      options: [
        {
          label: '上线',
          value: '1'
        },
        {
          label: '下线',
          value: '2'
        }
      ]
    }
  },
  {
    field: 'img_address',
    label: t('轮播图地址'),
    component: 'Input',
  },
  //轮播图类型
  {
    field: 'imgType',
    label: t('轮播图类型'),
    component: 'Select',
    formItemProps: {
      rules: [required()]
    },
    componentProps: {
      options: [
        {
          label: '商品',
          value: '1'
        },
        {
          label: '视频',
          value: '2'
        },
        {
          label: '其他',
          value: '3'
        }
      ]
    }
  },
  //开始时间
  {
    field: 'startDate',
    component: 'DatePicker',
    label: t('开始时间'),
    formItemProps: {
      rules: [required()]
    },
    componentProps: {
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  //结束时间
  {
    field: 'expireDate',
    component: 'DatePicker',
    label: t('结束时间'),
    formItemProps: {
      rules: [required()]
    },
    componentProps: {
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  //结束时间
  // {
  //   field: 'updateDate',
  //   component: 'DatePicker',
  //   label: t('更新时间'),
  //   formItemProps: {
  //     rules: [required()]
  //   },
  //   componentProps: {
  //     type: 'datetime',
  //     valueFormat: 'YYYY-MM-DD HH:mm:ss'
  //   }
  // },
  //备注
  {
    field: 'remarks',
    label: t('备注'),
    component: 'Input',
    colProps: {
      span: 24
    }
  }
])

const rules = reactive({
  title: [required()],
  author: [required()],
  importance: [required()],
  pageviews: [required()],
  display_time: [required()],
  content: [required()]
})

const { register, methods, elFormRef } = useForm({
  schema
})

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    const { setValues, setSchema } = methods
    setValues(currentRow)
    setSchema([
      {
        field: 'content',
        path: 'componentProps.defaultHtml',
        value: currentRow.content
      }
    ])
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  elFormRef,
  getFormData: methods.getFormData
})
</script>

<template>
  <Form :rules="rules" @register="register"/>

</template>
